package com.easyj.admin.edu.serviceimpl;

import org.activiti.api.process.model.ProcessInstance;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.easyj.admin.db.mapper.LeaveMapper;
import com.easyj.admin.db.model.Leave;
import com.easyj.base.enums.MutiApplyStatusEnum;

/**
 * 流程结束，对不需要2级审批的，修改状态
 * @author hjf
 *
 */
@Service("leaveApproveListener")
public class LeaveApproveListener implements ExecutionListener{

	private static final long serialVersionUID = 1L;
	
	@Autowired
    private ProcessRuntime processRuntime;
	
	@Autowired
	private  LeaveMapper leaveMapper;

	@Override
	@Transactional
	public void notify(DelegateExecution execution) {
	   String procId=execution.getProcessInstanceId();
	   ProcessInstance process=processRuntime.processInstance(procId);
	   Long entityId=Long.parseLong(process.getBusinessKey());
	   Leave find=leaveMapper.selectByPrimaryKey(entityId);
	   if(find.getStatus()==MutiApplyStatusEnum.WaitAudit.getKey()) {
		   Leave entity=new Leave();
		   entity.setId(entityId);
		   entity.setStatus(MutiApplyStatusEnum.PASSED.getKey());
		   leaveMapper.updateByPrimaryKeySelective(entity);
	   }
	}


}
